import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home'
import Patient from '../views/Patient'
import Order from '../views/Order'
import Mycenter from '../views/Mycenter'
import Search from '../views/Search'
import moredoctor from '../views/Home/moredoctor/MoreDoctor.vue'
import morehospital from '../views/Home/morehospital/MoreHospital.vue'
import doctorsInfo from '../views/Home/moredoctor/doctorsInfo/DoctorsInfo.vue'
import self from '../views/Mycenter/self/self.vue'
import login from '../views/RegorLgn/login.vue'
import register from '../views/RegorLgn/register.vue'
import addPaitent from '../views/Patient/addpatient/addPatient.vue'
import { Toast } from 'vant'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home',
    meta: {
      title: '预约挂号'
    },
    props: true
  },
  {
    path: '/home',
    component: Home,
    meta: {
      title: '预约挂号',
      isShowTabbar: true
    }
  },
  {
    path: '/order',
    component: Order,
    meta: {
      title: '挂号记录',
      isShowTabbar: true
    },
    props: true
  },
  {
    path: '/patient',
    component: Patient,
    meta: {
      title: '就诊人',
      isShowTabbar: true
    },
    props: true
  },
  {
    path: '/mycenter',
    component: Mycenter,
    meta: {
      title: '个人中心',
      isShowTabbar: true
    },
    props: true
  },
  {
    path: '/search',
    component: Search,
    props: true
  },
  {
    path: '/home/moredoctor',
    component: moredoctor,
    meta: {
      title: '医生列表',
      isShowTabbar: false
    },
    props: true
  },
  {
    path: '/home/doctorsInfo/:id',
    component: doctorsInfo,
    meta: {
      title: '医生详情',
      isShowTabbar: false
    },
    props: true
  },
  {
    path: '/home/morehospital',
    component: morehospital,
    meta: {
      title: '更多医院',
      isShowTabbar: false
    },
    props: true
  },
  {
    path: '/mycenter/self',
    component: self,
    meta: {
      title: '设置',
      isShowTabbar: false
    },
    props: true
  },
  {
    path: '/login',
    component: login,
    meta: {
      title: '登录',
      isShowTabbar: false
    },
    props: true
  },
  {
    path: '/register',
    component: register,
    meta: {
      title: '注册',
      isShowTabbar: false
    },
    props: true
  },
  {
    path: '/patient/addpaitent',
    component: addPaitent,
    meta: {
      title: '就诊人添加',
      isShowTabbar: false
    },
    props: true
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path !== '/login') {
    if (to.path === '/register') {
      next()
      return
    }
    const token = localStorage.getItem('Authorization')
    if (token) {
      next()
    } else {
      Toast('请先登录')
      next('/login')
    }
  } else {
    next()
  }
})

export default router
